package com.gljx.web.controller.JournalController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.User;
import com.gljx.web.Entity.JournalEntry.StudentDetail;
import com.gljx.web.Entity.JournalEntry.TrainCollect;
import com.gljx.web.service.StudentDetailService;
import com.gljx.web.service.TrainChargeService;

@Controller
@RequestMapping("/")
public class TrainCollectController {

	@Autowired
	private TrainChargeService trainChargeService;
	
	@Autowired
	private StudentDetailService studentDetailService;
	
	/**
	 * 查询汇总表一个月的数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryTrainCollect/look.action")
	public String queryTrainCollect(HttpServletRequest request){
		//获取当前月的第一天和最后一天
		Map<String, String> map = TimeFormat.thisMonth();
		request.setAttribute("beginTime", map.get("beginTime"));
		request.setAttribute("endTime", map.get("endTime"));
		return "/backHome/journaling/financialJournaling/trainCollect/collectList";
	}
	
	/**
	 * 根据时间查询数据
	 * @param request
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping("/queryCollectDataByTime")
	@ResponseBody
	public Map<String, Object> queryCollectDataByTime(HttpServletRequest request,String startTime,String endTime){
		//定义返回类型
		Map<String, Object> map = new HashMap<>();
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//计算两个时间的天数
		List<String> time = TimeFormat.getBetweenDays(startTime, endTime);
		map.put("time", time);
		//由于最后查询不到，所以加一天
		endTime = TimeFormat.getTomorrow(endTime);
		//查询培训收费
		List<TrainCollect> trainCollects = trainChargeService.queryTrainChargeByTime(user.getFcompanyid(), startTime, endTime);
		map.put("pxsf", trainCollects);
		//查询补考收费
		List<TrainCollect> bksf = trainChargeService.queryMakeupChargeByTime(user.getFcompanyid(), startTime, endTime);
		map.put("bksf", bksf);
		//查询转班收费单
		List<TrainCollect> zbsfd = trainChargeService.queryWillmigerlByTime(user.getFcompanyid(), startTime, endTime);
		map.put("zbsfd", zbsfd);
		//查询转班退费单
		List<TrainCollect> zbtfd = trainChargeService.queryTranSferByTime(user.getFcompanyid(), startTime, endTime);
		map.put("zbtfd", zbtfd);
		//查询规费
		List<TrainCollect> gf = trainChargeService.queryTurnexamCostByTime(user.getFcompanyid(), startTime, endTime);
		map.put("gf", gf);
		//查询计时卡
		List<TrainCollect> jsk = trainChargeService.queryTimeCardByTime(user.getFcompanyid(), startTime, endTime);
		map.put("jsk", jsk);
		//查询退、扣费金额
		List<TrainCollect>  tkfje = trainChargeService.queryReturnPremiumByTime(user.getFcompanyid(), startTime, endTime);
		map.put("tkfje", tkfje);
		//查询考前训练收费科目二
		List<TrainCollect> kqxlsfkm2 = trainChargeService.queryOwnCarTrainByTime(user.getFcompanyid(), startTime, endTime, 2);
		map.put("kqxlsfkm2", kqxlsfkm2);
		//查询考前训练收费科目二
		List<TrainCollect> kqxlsfkm3 = trainChargeService.queryOwnCarTrainByTime(user.getFcompanyid(), startTime, endTime, 3);
		map.put("kqxlsfkm3", kqxlsfkm3);
		//查询其他杂费
		List<TrainCollect> qtzf = trainChargeService.queryOtherPayDemandByTime(user.getFcompanyid(), startTime, endTime);
		map.put("qtzf", qtzf);
		return map;
	}
	
	/**
	 * 查询三天的明细表
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryTrainDetail/look.action")
	public String queryTrainDetail(HttpServletRequest request){
		//时间格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//获取当前时间
		String endTime = sdf.format(new Date());
		//获取前三天
		String beginTime = TimeFormat.getThreeDay();
		//把时间传到页面
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		return "/backHome/journaling/financialJournaling/trainCollect/detail";
	}
	
	/**
	 * 根据时间查询
	 * @param request
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping("/queryDetailDataByTime")
	@ResponseBody
	public List<StudentDetail> queryDetailDataByTime(HttpServletRequest request,String startTime,String endTime){
		//定义返回类型
		List<StudentDetail> studentDetailData = new ArrayList<>();
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//根据时间查询所有的学生
		List<StudentDetail> studentDetails = studentDetailService.queryStudentByTime(user.getFcompanyid(), startTime, endTime);
		if(studentDetails == null || studentDetails.size() == 0){
			
		}else{
			//已查询到了学生的编码，姓名，fid，身份证号，备注
			for (StudentDetail studentDetail : studentDetails) {
				String studentId = studentDetail.getFid();
				StudentDetail studentDetail2 = studentDetailService.queryStudentDataByFid(studentId);
				if(studentDetail2 == null){
					
				}else{
					studentDetail.setJfrq(studentDetail2.getJfrq());//缴费日期
					studentDetail.setPxf(studentDetail2.getPxf());//培训费
					studentDetail.setGf(studentDetail2.getGf());//规费
					//studentDetail.setQf(studentDetail2.getQf());//欠费
					studentDetail.setCash(studentDetail2.getCash());//现金
					studentDetail.setCard(studentDetail2.getCard());//刷卡
					studentDetail.setPxjsk(studentDetail2.getPxjsk());//培训计时卡
					studentDetail.setZx(studentDetail2.getZx());//照相
					studentDetail.setOther(studentDetail2.getOther());//其他
					studentDetail.setFpsj(studentDetail2.getFpsj());//发票时间
					studentDetail.setSjje(studentDetail2.getSjje());//上缴金额
					studentDetail.setFphm(studentDetail2.getFphm());//发票号码
					studentDetail.setFksj(studentDetail2.getFksj());//付款时间
					studentDetail.setTfpxf(studentDetail2.getTfpxf());//退费培训费
					studentDetail.setTgf(studentDetail2.getTgf());//退规费
					studentDetail.setTfpxjsk(studentDetail2.getTfpxjsk());//退费培训计时卡
					studentDetail.setTfsxf(studentDetail2.getTfsxf());//退费手续费
					studentDetail.setSksxf(studentDetail2.getSksxf());//刷卡手续费
					studentDetail.setJcf(studentDetail2.getJcf());//材料费
					studentDetail.setKkpxjsk(studentDetail2.getKkpxjsk());//扣款培训计时卡
					studentDetail.setKkqt(studentDetail2.getKkqt());//扣款其他
					studentDetail.setStje(studentDetail2.getStje());//实退金额
				}
				studentDetailData.add(studentDetail);
				//查询培训收费，学员的缴费日期，开票日期，现金，刷卡，培训费，规费，欠费
				/*List<StudentDetail> pxDetails = studentDetailService.queryTrainChargeByStudentId(studentId);
				//由于可能交了两次钱，所以要循环相加
				if(pxDetails == null || pxDetails.size() == 0){
					
				}else{
					BigDecimal cash = new BigDecimal(0);
					BigDecimal card = new BigDecimal(0);
					for (StudentDetail studentDetail2 : pxDetails) {
						if(studentDetail2.getCash() == null){
							
						}else{
							cash = cash.add(studentDetail2.getCash());
						}
						if(studentDetail2.getCard() == null){
							
						}else{
							card = card.add(studentDetail2.getCard());
						}
					}
					studentDetail.setJfrq(pxDetails.get(0).getJfrq());//缴费日期
					studentDetail.setPxf(pxDetails.get(0).getPxf());//培训费
					studentDetail.setGf(pxDetails.get(0).getGf());//规费
					studentDetail.setQf(pxDetails.get(0).getQf());//欠费
					studentDetail.setCash(cash);//现金
					studentDetail.setCard(card);//刷卡
				}
				//查询其他收费，培训计时卡，照相，其他
				StudentDetail otherPay = studentDetailService.queryOtherPayDemandByStudentId(studentId);
				if(otherPay == null){
					
				}else{
					studentDetail.setPxjsk(otherPay.getPxjsk());//培训计时卡
					studentDetail.setZx(otherPay.getZx());//照相
					studentDetail.setOther(otherPay.getOther());//其他
				}
				//查询发票时间,发票号码,付款时间，上缴金额
				StudentDetail turnexamCost = studentDetailService.queryTurnexamCostByStudentId(studentId);
				if(turnexamCost == null){
					
				}else{
					studentDetail.setFpsj(turnexamCost.getFpsj());//发票时间
					studentDetail.setSjje(turnexamCost.getSjje());//上缴金额
					studentDetail.setFphm(turnexamCost.getFphm());//发票号码
					studentDetail.setFksj(turnexamCost.getFksj());//付款时间
				}
				//查询培训费，退规费，培训计时卡，退费手续费，刷卡手续费，教材费，培训计时卡，其他 
				StudentDetail exitClazz = studentDetailService.queryExitClazzByStudentId(studentId);
				if(exitClazz == null){
					
				}else{
					studentDetail.setTfpxf(exitClazz.getTfpxf());//退费培训费
					studentDetail.setTgf(exitClazz.getTgf());//退规费
					studentDetail.setTfpxjsk(exitClazz.getTfpxjsk());//退费培训计时卡
					studentDetail.setTfsxf(exitClazz.getTfsxf());//退费手续费
					studentDetail.setSksxf(exitClazz.getSksxf());//刷卡手续费
					studentDetail.setJcf(exitClazz.getJcf());//材料费
					studentDetail.setKkpxjsk(exitClazz.getKkpxjsk());//扣款培训计时卡
					studentDetail.setKkqt(exitClazz.getKkqt());//扣款其他
					studentDetail.setStje(exitClazz.getStje());//实退金额
				}*/
			}
		}
		return studentDetailData;
	}
	
}
